package org.sninwo.lnbook.book.dao;

import org.sninwo.lnbook.base.hibernate.HibernateDao;
import org.sninwo.lnbook.book.entity.BookType;
import org.springframework.stereotype.Component;

@Component
public class BookTypeDao extends HibernateDao<BookType, Long> {
	/**
	 * 根据名称取类别，如果取不到则添加，并返回这个类别
	 */
	public BookType getOtherwiseAdd(String name){
		BookType bookType = findUniqueBy("name", name);
		if (bookType == null){
			bookType = new BookType(name, getMaxSeq() + 1);
		}
		save(bookType);
		return bookType;
	}
	
	public int getMaxSeq(){
		Object result = findUnique("select max(seq) from BookType");
		return result != null ? (Integer)result : 0;
	}
}
